﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Common.Activities.Design;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace Common.Activities
{
    [Designer(typeof(AddViewToRegionDesigner))]
    public class AddViewToRegion : NativeActivity
    {
        [RequiredArgument]
        public InArgument<string> ViewName { get; set; }
        [RequiredArgument]
        public InArgument<string> ViewType { get; set; }
        [RequiredArgument]
        public InArgument<string> RegionName { get; set; }
        public InArgument<string> ParameterName { get; set; }
        public InArgument<string> ParameterValue { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            //var container = context.Properties.Find("container") as IUnityContainer;

            // TODO: navigate to the requested view with any params
            var viewNameValue = context.GetValue(this.ViewName);
            var viewTypeValue = context.GetValue(this.ViewType);
            var regionNameValue = context.GetValue(this.RegionName);
            var parmName = context.GetValue(this.ParameterName);
            var parmValue = context.GetValue(this.ParameterValue);
            Helper.MarshalToUiThread(delegate
                                         {
                                             var regionManager = ServiceLocator.Current.TryResolve<IRegionManager>();
                                             //IRegionManager regionManager = container.Resolve<IRegionManager>();
                                             UriQuery parameters = new UriQuery();
                                             parameters.Add(parmName, parmValue);
                                             regionManager.RequestNavigate(regionNameValue,
                                                                           new Uri(
                                                                               viewNameValue + parameters.ToString(),
                                                                               UriKind.Relative));
                                         });

        }
    }
}
